<?php

class Common_DataTable_View_Html implements Common_DataTable_View {

    /**
     *
     * @var Common_DataTable_GroupRenderer
     */
    private $groupRenderer = null;

    public function setGroupRenderer(Common_DataTable_GroupRenderer $groupRenderer)
    {
        $this->groupRenderer = $groupRenderer;
    }

    /**
     *
     * @return Common_DataTable_GroupRenderer
     */
    public function getGroupRenderer()
    {
        return $this->groupRenderer;
    }

    public function publish($arr_data, $arr_headers, $arr_footers, $arr_properties, $arr_options = array()) {
        
        $str_output = null;
        
        if(!empty($arr_data)) {

            $obj_template = new Common_Template();

            $arr_options['group_renderer'] = $this->getGroupRenderer();

            $obj_template->assign(array(
                'arr_headers'    => $arr_headers,
                'arr_data'       => $arr_data,
                'arr_footers'    => $arr_footers,
                'arr_properties' => $arr_properties,
                'arr_options'    => $arr_options
            ));

           
            $str_output = $obj_template->render('datatable/default.phtml');
            
        }

        return $str_output;
        
    }

    static protected function getItem($data, $itemKey) {

        $items = explode('.', $itemKey);

        foreach ($items as $key) {
            if (is_array($data) && isset($data[$key])) {
                $data = $data[$key];
            } else {
                return null;
            }
        }

        return $data;

    }
    
    static public function generateRow($i, $str_row_key, $arr_row, $arr_columns, $arr_options = array()) {
        
        $arr_sorting = isset($arr_options['sorting']) ? $arr_options['sorting'] : array();
        
        $arr_css = array();
        if((($i+1)%2) == 0) {
            $arr_css[] = 'e';
        }
    
        if(isset($arr_options['row_class'])) {
            $arr_css[] = (string) $arr_options['row_class'];
        }
    
        $int_column_index = 0;
        if (isset($arr_options['row_renderer'])) {
            $rowRenderer = $arr_options['row_renderer'];
            echo $rowRenderer->render($i, $arr_row, $arr_options);
        } else {
            $str_css_class_row = !empty($arr_css) ? ' class="' . implode(' ', $arr_css) . '"' : '';
            echo '   <tr' . $str_css_class_row . '>' . "\n";
        }
        
        foreach($arr_columns as $str_key => $mix_header) {
    
            // standardise into an array of header options
            $arr_header = (array) $mix_header;
            
            //------------------------------------------------------------------
            
            // determine the data source, which defaults to the column key
            $str_source_key = isset($arr_header['source']) ? $arr_header['source'] : $str_key;
            
            // determine the data value
            $mix_value = self::getItem($arr_row, $str_source_key);
    
            // determine the data type
            $enm_type = isset($arr_header['type']) ? $arr_header['type'] : 'string';
    
            //------------------------------------------------------------------
            // determine the data class, which is used as the CSS class
            
            $arr_css_class = isset($arr_header['class']) ? (array) $arr_header['class'] : array();
    
            // if this column has a numeric class, then add another class to 
            // denote negative numbers
            
            if(in_array(COMMON_DATATABLE_CLASS_NUMBER, $arr_css_class) && ($mix_value < 0)) {
                $arr_css_class[] = COMMON_DATATABLE_CLASS_NEGATIVE;
            }

            $arr_meta_cell = array();
            if(isset($arr_meta[$str_row_key]) && isset($arr_meta[$str_row_key][$str_key])) {
                $arr_meta_cell = $arr_meta[$str_row_key][$str_key];
            }
    
            if(isset($arr_meta_cell['class'])) {
                $arr_css_class = array_merge($arr_css_class, $arr_meta_cell['class']);
            }
            
            if(isset($arr_sorting[$str_key])) {
                $str_class = COMMON_DATATABLE_CLASS_SORTED;
                if(count($arr_sorting) > 1) {
                    $int_index = Utility_ArrayFunctions::index_of_key($arr_sorting, $str_key);
                    $int_index++;
                    $str_class .= $int_index;
                }
                $arr_css_class[] = $str_class;
            }
    
            //------------------------------------------------------------------
            
            if(is_null($mix_value)) {
    
                $mix_value = COMMON_DATATABLE_NOT_APPLICABLE;
    
                if(isset($arr_header['default'])) {
                    $mix_value = $arr_header['default'];
                } else if($enm_type == 'special') {
                    $mix_value = null;
                }
    
            } else {
    
                if(isset($arr_header['format'])) {

                    $str_format_type = $arr_header['format'];
                    if(isset($arr_meta_cell['format'])) {
                        $str_format_type = $arr_meta_cell['format'];
                    }
    
                    if($str_format_type != 'html') {
    
                        $arr_format_options = isset($arr_header['format_options'])
                            ? $arr_header['format_options']
                            : array();
    
                        $mix_value = Utility_FormatFunctions::format_value($arr_header['format'], $mix_value, $arr_row, $arr_format_options);
                    }
    
                } else {
                    $mix_value = htmlentities($mix_value);
                }
                
            }
    
            $str_class = !empty($arr_css_class) ? 'class="' . implode(' ', $arr_css_class) . '" ' : '';
            
            echo '<td ' . $str_class . 'rel="col:' . $int_column_index . '">' . $mix_value . '</td>';
            
            $int_column_index++;
        }
        echo "\n" . '      </tr>' . "\n";
        
    }
    
}
